﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

using FLEN_Sim.funct;
using FLEN_Sim.gui.map;

using GMap.NET.WindowsForms;

namespace FLEN_Sim.containerobjects
{
    [Serializable()]

    /// <summary>
    /// Container-Klasse für Rettungswachen-Objekte.
    /// </summary>
    public class AmbulanceStation : IFLENObject
    {
        private string id, shortid, adress, city;
        private short citynumber;
        private MapMarkers.AmbulanceStationMarker marker;
        private List<AmbulanceVehicle> vehicles;
        private double lat, lon;

        /// <summary>
        /// Standard-Konstruktor.
        /// </summary>
        /// <param name="ID">Name des Krankenhauses</param>
        /// <param name="ShortID">gebräuchliche Abkürzung des Krankenhausnamens</param>
        /// <param name="Adress">Postanschrift des Krankenhauses</param>
        public AmbulanceStation(string ID, string ShortID, string Adress, ref GMapControl control)
        {
            id = ID;
            shortid = ShortID;
            adress = Adress;

            city = adress.Split(',')[1].Remove(0,1);

            citynumber = CityNumbers.getCityByName(city);

            GMap.NET.PointLatLng point = GeoCoder.getLocationNominatim(adress, citynumber);
            lat = point.Lat;
            lon = point.Lng;

            marker = new MapMarkers.AmbulanceStationMarker(point, MapMarkers.AmbulanceStationMarker.STATE_IN);
            marker.ToolTipMode = MarkerTooltipMode.OnMouseOver;
            marker.ToolTipText = id;

            vehicles = new List<AmbulanceVehicle>();
        }

        public AmbulanceStation(string ID, string ShortID, string Adress, double Lat, double Lon, ref GMapControl control)
        {
            id = ID;
            shortid = ShortID;
            adress = Adress;

            city = adress.Split(',')[1].Remove(0, 1);

            citynumber = CityNumbers.getCityByName(city);

            lat = Lat;
            lon = Lon;

            marker = new MapMarkers.AmbulanceStationMarker(new GMap.NET.PointLatLng(lat, lon), MapMarkers.AmbulanceStationMarker.STATE_IN);
            marker.ToolTipMode = MarkerTooltipMode.OnMouseOver;
            marker.ToolTipText = id;

            vehicles = new List<AmbulanceVehicle>();
        }

        public void addVehicle(AmbulanceVehicle toAdd)
        {
            if (!vehicles.Contains(toAdd))
                vehicles.Add(toAdd);
        }

        public AmbulanceVehicle getVehicleByCallsign(string callsign)
        {
            foreach (AmbulanceVehicle vehicle in vehicles)
            {
                if (vehicle.Callsign == callsign)
                    return vehicle;
            }

            return null;
        }

        public int checkState()
        {
            int vehiclesIn = 0, vehiclesOutOfTime = 0, vehiclesOut = 0;
            int stateBefore = this.marker.State;


            foreach (AmbulanceVehicle vehicle in vehicles)
            {
                switch (vehicle.Status)
                {
                    case 2:
                        vehiclesIn++;
                        break;
                    case 6:
                        vehiclesOutOfTime++;
                        break;
                    default:
                        vehiclesOut++;
                        break;
                }
            }

            if (vehiclesIn > 0)
                this.marker.State = MapMarkers.AmbulanceStationMarker.STATE_IN;
            else if (vehiclesOutOfTime == vehicles.Count)
                this.marker.State = MapMarkers.AmbulanceStationMarker.STATE_OUTOFTIME;
            else if (vehiclesOut > 0 && vehiclesIn == 0)
                this.marker.State = MapMarkers.AmbulanceStationMarker.STATE_ALLOUT;

            

            return this.marker.State;
        }

        /// <summary>
        /// Gibt den Namen der Wache zurück.
        /// </summary>
        public string ID
        {
            get
            {
                return id;
            }
        }

        /// <summary>
        /// Gibt die gebräuchliche Abkürzung der Wache zurück.
        /// </summary>
        public string ShortID
        {
            get
            {
                return shortid;
            }
        }

        /// <summary>
        /// Gibt die Anschrift der Wache zurück.
        /// </summary>
        public string Adress
        {
            get
            {
                return adress;
            }
        }

        /// <summary>
        /// Gibt die Stadt zurück, in der die Wache angesiedelt ist.
        /// </summary>
        public string City
        {
            get
            {
                return city;
            }
        }

        /// <summary>
        /// Gibt den Marker auf der Karte zurück.
        /// </summary>
        public MapMarkers.AmbulanceStationMarker Marker
        {
            get
            {
                return marker;
            }
            set
            {
                marker = value;
            }
        }

        public List<AmbulanceVehicle> Vehicles
        {
            get
            {
                return vehicles;
            }
        }
    }
}
